<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    html,body{
      width: 100%;
      height: 100%;
    }
    .top_btn{
      font-size: 80px;
      font-weight: 700;
      position: absolute;
      left:50%;
      transform: translateX(-50%);
      color: #666;
      cursor: pointer;
    }
    form{
      width: 460px;
      height: 340px;
      position: absolute;
      left:50%;
      top:50%;
      transform: translateX(-50%) translateY(-50%);
      text-align: center;
      box-shadow: 0 0 10px rgb(250, 249, 249);
      background-color: white;
      display: none;
      z-index: 2;
      cursor: move;
    }
    .user{
      margin-top:80px;
      margin-bottom: 30px;
      padding: 0 30px;
    }
    input{
      width: 300px;
      height: 25px;
      padding-left: 6px;
      outline-style: none;
    }
    label{
     font-size: 16px;
    }
    .user label{
      padding-left: 10px;
    }
    span{
      display: block;
      font-size: 12px;
      color:red;
      padding-right: 150px;
      display: none;
    }
    .button{
      width: 200px;
      height: 40px;
      background-color: rgb(143, 184, 231);
      border: none;
      outline-style: none;
      color:#666;
      line-height: 40px;
      text-align: center;
      margin-top: 30px;
    }
    .close{
      position: absolute;
      right: -150px;
      top:-18px;
      font-size: 14px;
      color: white;
      text-shadow: 0 0 5px white;
      display: block;
      cursor:pointer;
    }
    .model{
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.5);
      position: absolute;
      z-index: 1;
      display: none;
    }
  </style>
</head>
<body>
  <div class="top_btn">点击用户登录</div>

  <form action="">
    <div class="user">
      <label for="">用户名:</label>
      <input type="text" placeholder="请输入用户名" autofocus>
      <span>用户名不能为空</span>
    </div>
    <div class="psw">
      <label for="">登录密码:</label>
      <input type="text" placeholder="请输入密码" autofocus>
      <span>密码不能为空</span>
    </div>
    <input type="button" value="用户登录" class="button">
    <span class="close">关闭</span>
  </form>
  <div class="model"></div>
  <script>
    var top_btn = document.querySelector('.top_btn');
    var model = document.querySelector('.model');
    var form = document.querySelector('form');
    var close = document.querySelector('.close');
    var login = document.querySelector('.button');
    var u_name = document.querySelector('.user input');
    var pw = document.querySelector('.psw input');
    var red_name = document.querySelector('.user span');
    var red_psw = document.querySelector('.psw span');
    console.log(login);
    top_btn.onclick = function(){
      model.style.display = 'block';
      form.style.display = 'block';
    };
    form.onmousedown = function(e){
      var mouse_x = e.clientX - this.offsetLeft;
      var mouse_y = e.clientY - this.offsetTop;
      document.onmousemove = function(e){
        form.style.left = e.pageX - mouse_x + 'px';
        form.style.top = e.pageY - mouse_y + 'px';
      };
    };
    form.onmouseup = function(){
      document.onmousemove = null;
    }
    close.onclick = function(){
      if(confirm('确定要关闭么')){
        model.style.display = 'none';
        form.style.display = 'none';
        u_name.value = '';
        pw.value = '';
        red_name.style.display = 'none';
        red_psw.style.display = 'none';
      };
    };
    
    login.onclick = function(){
      var flag = true;
      if(u_name.value.length == 0){
        red_name.style.display = 'block';
        flag = false;
      }
      if(pw.value.length == 0 ){
        red_psw.style.display = 'block';
        flag = false;
      }
     if(flag){
        model.style.display = 'none';
        form.style.display = 'none';
        u_name.value = '';
        pw.value = '';
        red_name.style.display = 'none';
        red_psw.style.display = 'none';
     }
    }
  </script>
</body>
</html>